﻿package com.eitb.plantillas{
	import flash.net.*;
	import flash.geom.*;
	import flash.utils.*;
	import flash.events.*;
	import flash.display.*;
	import com.eitb.factorias.*;
	import com.eitb.plantillas.*;
	import com.eitb.cargadores.*;
	import com.eitb.events.*;
	import com.eitb.net.*;
	import com.eitb.formaters.*;
	import com.eitb.events.ScrollEvent;
	import flash.utils.*;
	import flash.media.*;
	import flash.net.*;
	import com.eitb.nielsen.*;
	import com.eitb.rutas.*;
//	import com.eitb.programas.menu.utils.*;	
	public class ModeloReproductor implements IControladorPlantilla,IControladorPlantillaReproductor {
		private var REFRESCO_DISPLAY:int = 500;// quinientos milisegunds
		private var vPlantilla:IVistaPlantilla;
		private var vPlantillaReproductor:IVistaPlantillaReproductor;
		private var urlVideo:URLRequest;

		private var tmrDisplay:Timer;
		private var textosXml:XMLList;
		private var vPlantillaReproductorTexto:IVistaPlantillaTexto;
		private var connection:NetConnection;
		private var stream:NetStreamHandler;

		public function ModeloReproductor():void {
			this.tmrDisplay = new Timer(REFRESCO_DISPLAY);
		}
		public function initVistaPlantilla(vPlantilla:IVistaPlantilla):void {
			this.vPlantilla = vPlantilla;
		}

		public function initVistaPlantillaReproductor(vPlantillaReproductor:IVistaPlantillaReproductor):void {
			this.vPlantillaReproductor = vPlantillaReproductor;
		}
		public function initVistaPlantillaReproductorTexto(vPlantillaReproductorTexto:IVistaPlantillaTexto):void {
			this.vPlantillaReproductorTexto = vPlantillaReproductorTexto;
		}
		public function initUrlVideo(url:URLRequest):void {
			this.urlVideo = new Rutas().getRutaVideo(url.url);
		}
		public function readyVideo():void {
			this.vPlantillaReproductor.dispatchPlayEvent(new MouseEvent(MouseEvent.CLICK));
			this.vPlantillaReproductor.dispatchBotonAtrasEvent(new MouseEvent(MouseEvent.CLICK));
		}
		// BOTONES 
		public function clickBotonPlay(event:MouseEvent):void {
			this.vPlantillaReproductor.setBotonPauseVisible(true);
			this.vPlantillaReproductor.setBotonAtrasVisible(true);
			this.vPlantillaReproductor.setBotonSonidoVisible(true);
			this.vPlantillaReproductor.setBotonPlayVisible(false);

			if (null == this.connection) {
				this.connection = new NetConnection();
				this.connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
				this.connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
				this.connection.connect(null);
			} else {
				this.stream.resume();
			}
		}

		public function clickBotonPause(event:MouseEvent):void {
			this.vPlantillaReproductor.setBotonPlayVisible(true);
			this.vPlantillaReproductor.setBotonPauseVisible(false);
			this.stream.pause();
		}

		public function clickBotonAtras(event:MouseEvent):void {
			this.vPlantillaReproductor.setBotonPlayVisible(true);
			this.vPlantillaReproductor.setBotonPauseVisible(false);
			this.stream.pause();
			this.stream.seek(0);
			this.vPlantillaReproductor.setTiempoTranscurrido("");
			this.vPlantillaReproductor.setTiempoTotal("");
		}
		public function clickBotonSonido(event:MouseEvent):void {
			this.vPlantillaReproductor.setScrollRojoVisible(true);
		}
		public function upBotonScroll(event:ScrollEvent):void {
			this.stream.soundTransform = new SoundTransform(event.porcentaje);
		}
		// BOTONES

		// VIDEO
		private function netStatusHandler(event:NetStatusEvent):void {
			switch (event.info.code) {
				case "NetConnection.Connect.Success" :
					connectStream();
					new NielsenCall("reproductorStart_".concat(this.urlVideo.url)).doPing();
					break;
				case "NetStream.Play.StreamNotFound" :
					trace("Unable to locate video: " + this.urlVideo);
					break;
			}
		}

		private function connectStream():void {
			this.stream = new NetStreamHandler(this.connection);
			this.stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			this.stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
			this.stream.addEventListener(OnMetaDataEvent.ON_META_DATA, onMetaDataHandler);
			this.vPlantillaReproductor.attachNetStream(this.stream);
			this.stream.play(this.urlVideo.url);

			tmrDisplay.addEventListener(TimerEvent.TIMER, refrescarDisplay);
		}

		private function securityErrorHandler(event:SecurityErrorEvent):void {
			trace("securityErrorHandler: " + event);
		}
		private function asyncErrorHandler(event:AsyncErrorEvent):void {
			// ignore AsyncErrorEvent events.
		}
		private function refrescarDisplay(event:TimerEvent):void {
			this.vPlantillaReproductor.setTiempoTranscurrido(new TimerFormater(new Number(this.stream.time), TimerFormater.SEGUNDOS).toClock());
		}
		function onMetaDataHandler(event:OnMetaDataEvent):void {
			this.vPlantillaReproductor.setTiempoTotal(new TimerFormater(new Number(event.duration), TimerFormater.SEGUNDOS).toClock());
			this.tmrDisplay.start();
		}
		// VIDEO
		// TEXTOS
		public function initTextos(textosXml:XMLList):void {
			this.textosXml=textosXml;
			this.vPlantillaReproductorTexto.setTextos(textosXml);
		}
	}
}